package com.fastjrun.ciagent.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipFileHelper {
    /**
     * 对zipBeforeFile目录下的文件压缩，保存为指定的文件zipAfterFile
     * 
     * @param zipBeforeFile
     *            压缩之前的文件
     * @param zipAfterFile
     *            压缩之后的文件
     */
    public static void zip(String zipBeforeFile, String zipAfterFile) {
        try {

            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(mkdirFiles(zipAfterFile)));
            File fromfile = new File(zipBeforeFile);
            fileZip(zos, fromfile);
            zos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 解压缩文件unZipBeforeFile保存在unZipDirectory目录下
     * 
     * @param unZipBeforeFile
     *            解压之前的文件
     * @param unZipDirectory
     *            解压之后的文件夹
     * @throws IOException
     */
    public static String unZipFileInToDirectory(String unZipBeforeFile, String unZipDirectory) throws Exception {
        ZipFile zipFile = new ZipFile(unZipBeforeFile);
        Enumeration files = zipFile.entries();
        String rootName="";
        File f = null;
        FileOutputStream fos = null;

        while (files.hasMoreElements()) {
            try {
                ZipEntry entry = (ZipEntry) files.nextElement();
                if(rootName.equals("")){
                    rootName=entry.getName();
                }
                InputStream eis = zipFile.getInputStream(entry);
                byte[] buffer = new byte[1024];
                int bytesRead = 0;

                f = new File(new File(unZipDirectory).getAbsolutePath() + File.separator + entry.getName());

                if (entry.isDirectory()) {
                    f.mkdirs();
                    continue;
                } else {
                    f.getParentFile().mkdirs();
                    f.createNewFile();
                }

                fos = new FileOutputStream(f);

                while ((bytesRead = eis.read(buffer)) != -1) {
                    fos.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        }
        return rootName;

    }

    /**
     * 根据filePath创建相应的目录
     * 
     * @param filePath
     * @return
     * @throws IOException
     */
    private static File mkdirFiles(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        return file;
    }

    /**
     * 压缩文件
     * 
     * @param zos
     * @param file
     * @throws Exception
     */
    private static void fileZip(ZipOutputStream zos, File file) throws Exception {
        if (file.isFile()) {
            zos.putNextEntry(new ZipEntry(file.getName()));
            FileInputStream fis = new FileInputStream(file);
            byte[] bb = new byte[10240];
            int aa = 0;
            while ((aa = fis.read(bb)) != -1) {
                zos.write(bb, 0, aa);
            }
            fis.close();
        } else {
            directoryZip(zos, file, file.getName());
        }
    }

    /**
     * 添加到压缩文件中
     * 
     * @param out
     * @param f
     * @param base
     * @throws Exception
     */
    private static void directoryZip(ZipOutputStream out, File f, String base) throws Exception {
        // 如果传入的是目录
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            // 创建压缩的子目录
            out.putNextEntry(new ZipEntry(base + "/"));
            if (base.length() == 0) {
                base = "";
            } else {
                base = base + "/";
            }
            for (int i = 0; i < fl.length; i++) {
                directoryZip(out, fl[i], base + fl[i].getName());
            }
        } else {
            // 把压缩文件加入zip中
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            byte[] bb = new byte[10240];
            int aa = 0;
            while ((aa = in.read(bb)) != -1) {
                out.write(bb, 0, aa);
            }
            in.close();
        }
    }

    /**
     * 解压缩文件
     * 
     * @param zis
     * @param file
     * @throws Exception
     */
    private static void fileUnZip(ZipInputStream zis, File file) throws Exception {
        ZipEntry zip = zis.getNextEntry();
        if (zip == null)
            return;
        // 是文件，解压缩
        if (!zip.isDirectory()) {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            byte b[] = new byte[10240];
            int aa = 0;
            while ((aa = zis.read(b)) != -1) {
                fos.write(b, 0, aa);
            }
            fos.close();
        }
    }
}
